fix(send): keep amount math in integer minor units, sanitize the amount field (closes #457) - #545
Open
Vyacheslav-Tomashevskiy wants to merge 1 commit into
Conversation
parseFloat() on the amount string turned every typed amount into an IEEE754 double before it was multiplied by the rate and rendered with toFixed(2) - three places for binary rounding error on a value the user is about to send irreversibly over Stellar. - add src/lib/currency.ts: toMinorUnits/fromMinorUnits do the decimal <-> integer-minor-unit conversion by string manipulation, never via parseFloat; convertMinorUnits rounds through the rate exactly once - wire send/page.tsx's amount, conversion display and canContinue gate through it - sanitize the amount field as it is typed, and make it a text field with inputMode="decimal" so its value is exactly the characters the user typed (type="number" also accepts scientific notation) - 32 unit tests for the new helpers
|
Someone is attempting to deploy a commit to the codex723's projects Team on Vercel. A member of the Team first needs to authorize it. |
5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #457.
The bug
send/page.tsxdid all of its amount math in floating point:That is three separate places for binary rounding error to appear on a value the user is about to send irreversibly over Stellar: the
parseFloatof the typed amount, the multiplication, andtoFixed(2)((1.005).toFixed(2)is"1.00"in every JS engine, not"1.01"). On top of that the field wastype="number", whose value the browser also considers valid for scientific notation (1e5) and unbounded decimal places, so the string posted to/api/stellar/sendcould disagree with what the user believed they typed.The fix
New
src/lib/currency.tskeeps the amount as an exact integer count of minor units:toMinorUnits("100.50") === 10050— done by splitting the decimal string and concatenating digits, never by parsing a float. Returnsnullfor anything that isn't a plain non-negative decimal, or that has more fractional digits than the currency supports (rejecting beats silently truncating a user's money).fromMinorUnits(10050) === "100.50"— the inverse, integer arithmetic only.convertMinorUnits(amountMinor, rate)— the rate is inherently a float from the API, so the multiply stays a multiply, but it rounds to the nearest minor unit exactly once and the amount side of it was never anything but an exact integer.sanitizeAmountInputconstrains the field as the user types (digits and one decimal point, capped at the currency's precision), so what is displayed is always exactly whattoMinorUnitswill accept.send/page.tsxnow routes the amount, the "Recipient Gets" display, the summary card and thecanContinuegate through those helpers, and the amount field becametype="text"+inputMode="decimal"(still a numeric keypad on mobile, but the value is exactly the characters typed) with anaria-label.Scope note (deviation from the issue text, as requested in the issue's contributor notes)
The issue suggests "consider a shared component". I did the shared logic rather than a shared visual component: a new input component would mean reworking
send/page.tsx's existing Tailwind layout well beyond what this precision bug needs, and the precision problem lives entirely in the math, not the markup.rates/page.tsxis listed in the issue too, but it has no user-entered amount at all — everyparseFloatthere is read-only formatting of server-provided rate/liquidity strings for display, so there is nothing to fix there. Happy to widen the scope to an actual<AmountInput>component if you'd prefer it.Verification
src/lib/__tests__/currency.test.ts, all green (npx vitest run src/lib/__tests__/currency.test.ts) — including the1.005/toFixedcase, over-precision rejection,Number.isSafeIntegerguard, custom-precision and zero-decimal currencies.npx eslinton all three touched files: clean.npm run build:✓ Compiled successfully, and the type-check step passes for everything in this PR. (The build's TypeScript stage still stops on the pre-existing missingisValidStellarPublicKeyimport insrc/lib/validations.ts, unrelated to this change — that's what fix(validations): restore broken isValidStellarPublicKey import; feat: add /api/stellar/fee-estimate #529 fixes. I temporarily added that import locally to get the build past it and confirm a full clean pass, then reverted it; it is not part of this diff.)12ab.3456e7into the amount field yields12.34— letters dropped, no scientific notation, precision capped. Layout at 375px is byte-identical tomain(same element geometry; the card's pre-existing horizontal overflow at that width is unchanged by this PR).tsc --noEmitreports the same pre-existing errors as cleanmainand no new ones.